package com.stock.demo.function.share.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.stock.demo.R;
import com.stock.demo.adapter.StockPositionAdapter;
import com.stock.demo.framework.ui.activity.BaseActivity;
import com.stock.demo.model.MarketDetailBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class StockPositionActivity extends BaseActivity implements StockPositionView {

    private TabLayout mTlIndex;
    private ViewPager mVpContent;
    private String[] mTitles = {"买入", "卖出", "撤单", "查询"};
    private StockPositionAdapter mStockPositionAdapter;
    private static final String INTENT_CHECK_POSITION_KEY = "checkPosition";
    private static final String INTENT_DETAIL_BEAN_KEY = "detailBean";
    private int mCheckIndex;
    private MarketDetailBean mMarketDetailBean;

    public static void startAction(Context context, int checkPosition, MarketDetailBean bean) {
        Intent intent = new Intent(context, StockPositionActivity.class);
        intent.putExtra(INTENT_CHECK_POSITION_KEY, checkPosition);
        intent.putExtra(INTENT_DETAIL_BEAN_KEY, bean);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_position);
        getIntentMessage();
        initView();
    }

    private void getIntentMessage() {
        Intent intent = getIntent();
        mCheckIndex = intent.getIntExtra(INTENT_CHECK_POSITION_KEY, 0);
        Serializable extra = intent.getSerializableExtra(INTENT_DETAIL_BEAN_KEY);
        if (extra != null && extra instanceof MarketDetailBean) {
            mMarketDetailBean = (MarketDetailBean) extra;
        }
    }

    private void initView() {
        if (mMarketDetailBean != null) {
            TextView tvTitle = (TextView) findViewById(R.id.tv_stock_position_title);
            tvTitle.setText(mMarketDetailBean.mName + "-" + mMarketDetailBean.mSymbol);
        }
        findViewById(R.id.iv_stock_position_finish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTlIndex = (TabLayout) findViewById(R.id.tl_stock_position);
        mVpContent = (ViewPager) findViewById(R.id.vp_stock_position);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new BuyFragment());
        fragments.add(new SellFragment());
        fragments.add(new CancelFragment());
        fragments.add(new QueryFragment());
        mStockPositionAdapter = new StockPositionAdapter(getSupportFragmentManager(), fragments, mTitles);
        mVpContent.setAdapter(mStockPositionAdapter);
        mTlIndex.setTabMode(TabLayout.MODE_FIXED);
        mTlIndex.addTab(mTlIndex.newTab().setText(mTitles[0]));
        mTlIndex.addTab(mTlIndex.newTab().setText(mTitles[1]));
        mTlIndex.addTab(mTlIndex.newTab().setText(mTitles[2]));
        mTlIndex.addTab(mTlIndex.newTab().setText(mTitles[3]));
        mTlIndex.setupWithViewPager(mVpContent);
        mTlIndex.setTabsFromPagerAdapter(mStockPositionAdapter);
        mVpContent.setCurrentItem(mCheckIndex);
    }
}
